package com.wanlian.router.ui.manage;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.smart.android.ui.BaseFragment;
import com.smart.android.ui.app.IntentExtra;
import com.smart.android.widget.EditTextWithClear;
import com.wanlian.router.R;
import com.wanlian.router.bean.RouterInfo;
import com.wanlian.router.bean.WifiSetting;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;
import com.wanlian.router.ui.dialog.EncryptTypeDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/9/8.
 * Email: fvaryu@163.com
 */
public class WifiSettingFragment extends BaseFragment implements TextWatcher {
    

    @BindView(R.id.et_ssid)
    EditTextWithClear et_ssid;
    @BindView(R.id.et_pwd)
    EditTextWithClear et_pwd;

    @BindView(R.id.cb_hidden)
    Switch cb_hidden;
    @BindView(R.id.image_check)
    ImageView mCheckEye;
    @BindView(R.id.tv_next)
    TextView tv_next;
    @BindView(R.id.tv_encrypt)
    TextView mEncrypt;
    private boolean showPwd;

    public static WifiSettingFragment newInstance(int type) {

        Bundle args = new Bundle();
        args.putInt(IntentExtra.EXTRA_TYPE, type);
        WifiSettingFragment fragment = new WifiSettingFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int layout() {
        return R.layout.fragment_wifi_setting;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);
        et_ssid.addTextChangedListener(this);
        et_pwd.addTextChangedListener(this);
    }

    @Override
    protected void initData(View view) {
        super.initData(view);
        mEncrypt.setText(WifiSetting.TYPES[0]);

        NetWorkingClient.getInstance().queryCurrentRouterInfo(getActivity(), new INetCallBack<RouterInfo>() {
            @Override
            public void onFinish(ResponseData response, @Nullable RouterInfo info) {
                if (response.isSuccess()) {
                    if (info != null) {
                        if (info.getDeviceConfig() == null) {
                            return;
                        }
                        WifiSetting wifiSetting = info.getDeviceConfig();
                        if (getArguments().getInt(IntentExtra.EXTRA_TYPE) == 0) {
                            et_ssid.setText(wifiSetting.getTwoWifiAccount());
                            et_pwd.setText(wifiSetting.getTwoWifiPassWord());
                            mEncrypt.setText(WifiSetting.getUIPWDType(wifiSetting.getTwoWifiLocktype()));
                            cb_hidden.setChecked(wifiSetting.isTwoWifiForce());
                        } else if (getArguments().getInt(IntentExtra.EXTRA_TYPE) == 1) {
                            et_ssid.setText(wifiSetting.getFiveAccount());
                            et_pwd.setText(wifiSetting.getFiveWifiPassWord());
                            mEncrypt.setText(WifiSetting.getUIPWDType(wifiSetting.getFiveWifiLocktype()));
                            cb_hidden.setChecked(wifiSetting.isFiveWifiForce());
                        } else {
                            et_ssid.setText(wifiSetting.getCustomerWifiAccount());
                            et_pwd.setText(wifiSetting.getCustomerWifiPassWord());
                            mEncrypt.setText(WifiSetting.getUIPWDType(wifiSetting.getCustomerWifiLocktype()));
                            cb_hidden.setChecked(wifiSetting.isCustomerWifiForce());
                        }


                    }
                }
            }
        });
    }

    @OnClick(R.id.tv_encrypt)
    void encrypt() {

        EncryptTypeDialog encryptTypeDialog = new EncryptTypeDialog(getActivity());
        encryptTypeDialog.setOnClickListener(new EncryptTypeDialog.OnClickListener() {
            @Override
            public void click(String a) {

                mEncrypt.setText(WifiSetting.getUIPWDType(a));
            }
        });
        encryptTypeDialog.show();
    }

    @OnClick(R.id.image_check)
    void check() {
        showPwd = !showPwd;
        if (showPwd) {
            mCheckEye.setImageResource(R.drawable.eye_open_gray);
            et_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            mCheckEye.setImageResource(R.drawable.eye_close_gray);
            et_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }


    }

    @OnClick(R.id.tv_next)
    void next() {
        WifiSetting wifiSetting = new WifiSetting();
        int a = getArguments().getInt(IntentExtra.EXTRA_TYPE);
        if (a == 0) {
            wifiSetting.setHastwoConfig(true);
            wifiSetting.setTwoWifiAccount(et_ssid.getText().toString());
            wifiSetting.setTwoWifiPassWord(et_pwd.getText().toString());
            wifiSetting.setTwoWifiForce(cb_hidden.isChecked());
            wifiSetting.setTwoWifiLocktype(WifiSetting.toPWD(mEncrypt.getText().toString()));
        } else if (a == 1) {
            wifiSetting.setHasFiveConfig(true);
            wifiSetting.setFiveAccount(et_ssid.getText().toString());
            wifiSetting.setFiveWifiPassWord(et_pwd.getText().toString());
            wifiSetting.setFiveWifiForce(cb_hidden.isChecked());
            wifiSetting.setFiveWifiLocktype(WifiSetting.toPWD(mEncrypt.getText().toString()));
        } else {
            wifiSetting.setHasCustomerConfig(true);
            wifiSetting.setCustomerWifiAccount(et_ssid.getText().toString());
            wifiSetting.setCustomerWifiPassWord(et_pwd.getText().toString());
            wifiSetting.setCustomerWifiForce(cb_hidden.isChecked());
            wifiSetting.setCustomerWifiLocktype(WifiSetting.toPWD(mEncrypt.getText().toString()));
        }
        NetWorkingClient.getInstance().settingWifi(getActivity(), false,true, wifiSetting, new INetCallBack() {
            @Override
            public void onFinish(ResponseData response, @Nullable Object o) {
                if (response.isSuccess()) {

                    showToast("保存成功");
                } else {
                    showToast(response.getMessage());
                }
            }
        });
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        tv_next.setEnabled(et_ssid.getText() != null && !TextUtils.isEmpty(et_ssid.getText().toString()) &&
                et_pwd.getText() != null && !TextUtils.isEmpty(et_pwd.getText().toString()));
    }
}
